package com.example.mylibrary.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.example.mylibrary.ActivityManager
import com.lxj.xpopup.XPopup

abstract class BaseActivity<B : ViewBinding> : AppCompatActivity(), BaseView, View.OnClickListener {
    var bundleExtra: Bundle? = null
    lateinit var viewBinding: B

    val builder: XPopup.Builder by lazy {
        XPopup.Builder(this)
    }

    /**
     * 开启下个页面
     */
    fun startNextActivity(context: Context, bundle: Bundle?, clazz: Class<*>) {
        val intent = Intent(context, clazz)
        intent.putExtra("bundle", bundle)

        startNextActivity(intent)
    }

    private fun startNextActivity(intent: Intent) {
        startActivity(intent)
    }

    /**
     * 开启下个页面，请求结果
     */
    fun startNextActivityForResult(
        context: Context,
        requestCode: Int,
        bundle: Bundle?,
        clazz: Class<*>
    ) {
        val intent = Intent(context, clazz)
        intent.putExtra("bundle", bundle)

        startActivityForResult(intent, requestCode)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityManager.addActivity(this)
        viewBinding = getBinding()
        setContentView(viewBinding.root)
        bundleExtra = intent.getBundleExtra("bundle")
        // 初始化布局
        initView()
        // 初始化数据
        initData()
    }

    /**
     * 通过 viewbinding 进行视图绑定
     */
    abstract fun getBinding(): B

    /**
     * 初始化view
     */
    abstract fun initView()

    /**
     * 初始化数据
     */
    abstract fun initData()

    protected open fun viewVisible(vararg views: View) {
        for (view in views) {
            view.visibility = View.VISIBLE
        }
    }

    protected open fun viewGone(vararg views: View) {
        for (view in views) {
            view.visibility = View.GONE
        }
    }

    override fun onResume() {
        super.onResume()

    }

    override fun onPause() {
        super.onPause()

    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityManager.finishActivity(this)
    }

    /**
     * 初始化view(可设置点击事件,调用findviewbyid方法, 此方法在setContentView后执行
     */

    /**
     * 复写了点击事件，子类根据需求实现
     */
    override fun onClick(p0: View?) {
        // TODO("复写了点击事件，子类根据需求实现")
    }


    override fun showLoading() {
        //TODO("Not yet implemented")
    }

    override fun showSuccess() {
        //TODO("Not yet implemented")
    }

    override fun showFailed() {
        //TODO("Not yet implemented")
    }

    override fun showNoNet() {
        //TODO("Not yet implemented")
    }

    override fun showOnRetry() {
        //TODO("Not yet implemented")
    }
}